package com.bulemobi.activity.ac_login;

import android.graphics.Paint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.bulemobi.activity.ac_home_page.Ac_HomePage;
import com.bulemobi.data.DataCentre;
import com.bulemobi.net.tsz.afinal.FinalHttp;
import com.bulemobi.net.tsz.afinal.annotation.view.ViewInject;
import com.bulemobi.net.tsz.afinal.http.AjaxCallBack;
import com.bulemobi.net.tsz.afinal.http.AjaxParams;
import com.bulemobi.urlCentre.UrlCentre;
import com.bulemobi.util.VerifyUtil;
import com.bulemobi.view.BaseActivity;
import com.bulemobi.view.AllView;
import com.bulemobi.view.titleView.ActionBarView;
import com.jsyh.hcb.R;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 武 on 2014/9/16 0016.
 * 用户登陆
 */
public class Ac_Login extends BaseActivity implements View.OnClickListener {
    private AllView allView = new AllView();
    @ViewInject(id = R.id.actionBar)
    ActionBarView actionBarView;
    @ViewInject(id = R.id.ac_bt_login)
    TextView ac_bt_login;
    @ViewInject(id = R.id.ed_userName)
    EditText ed_userName;
    @ViewInject(id = R.id.ed_passWord)
    EditText ed_passWord;
    @ViewInject(id = R.id.tv_forgetPassWord)
    TextView tv_forgetPassWord;
    //时时监听 注册 按钮背景色的改变
    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (TextUtils.isEmpty(s.toString())
                    && !VerifyUtil.isMobileNO(ed_userName.getText().toString().trim())) {
                ac_bt_login.setBackgroundResource(R.drawable.gray_shape);
                ac_bt_login.setClickable(false);
                return;
            } else if (ed_passWord.getText().length() <= 0) {
                ac_bt_login.setBackgroundResource(R.drawable.gray_shape);
                ac_bt_login.setClickable(false);
                return;
            } else {
                ac_bt_login.setClickable(true);
                ac_bt_login.setBackgroundResource(R.drawable.green_shape);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_login_main);
        initView();
        findActionBar();
        //设置下划线
        tv_forgetPassWord.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
    }

    private void findActionBar() {
        actionBarView.title.setText(getResources().getText(R.string.login));
        actionBarView.button_right_text.setVisibility(View.VISIBLE);
        actionBarView.button_right_text.setText(getResources().getText(R.string.register));
        actionBarView.button_right_text.setTextColor(getResources().getColor(R.color.white));
        actionBarView.button_right_text.setOnClickListener(this);
    }

    private void initView() {
        allView.ac_tv_forgetPassWord = (TextView) findViewById(R.id.tv_forgetPassWord);
        allView.ac_tv_forgetPassWord.setOnClickListener(this);
        allView.ac_ed_userName = (EditText) findViewById(R.id.ed_userName);
        allView.ac_ed_passWord = (EditText) findViewById(R.id.ed_passWord);
        ac_bt_login.setOnClickListener(this);
        ac_bt_login.setBackgroundResource(R.drawable.gray_shape);
        ed_passWord.addTextChangedListener(watcher);
        ed_userName.addTextChangedListener(watcher);

        //如果默认没有输入的值，则没有点击事件
        ac_bt_login.setClickable(false);
    }

    private void requestHttp(String url, AjaxParams params) {
        final FinalHttp http = new FinalHttp();
        http.post(url, params, new AjaxCallBack<Object>() {
            @Override
            public void onStart() {
                showProgressDialog();
                super.onStart();
            }

            @Override
            public void onSuccess(Object o) {
                Log.d("OnSuccess",o.toString());
                closeProgressDialog();
                try {
                    JSONObject object = new JSONObject(o.toString());
                    if (object.getString("_RejCode").equals(DataCentre.RIGHT_RejCode)) {
                        DataCentre.isLogin = true;
                        openActivity(Ac_HomePage.class);
                    } else {
                        showToastShort("用户名或密码错误");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.d("OnSuccess", o.toString());
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                closeProgressDialog();
                showToastShort("网络异常");
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_forgetPassWord:
                openActivity(Ac_ForgetPassword.class);
                break;
            case R.id.ac_bt_login:
                AjaxParams params = new AjaxParams();
                String passWord;
//                TODO:这里的参数先写死们最后要修改
                params.put("UserId", ed_userName.getText().toString());
//                params.put("UserId", "18049213481");
                params.put("Password", ed_passWord.getText().toString());
//                params.put("Password", "admin123");
                params.put("LoginType", "1");
                params.put("TerminalType", UrlCentre.TerminalType);
                params.put("UserType", UrlCentre.UserType);
                params.put("ChannelId", UrlCentre.ChannelId);
                params.put("BankId", DataCentre.BankId);
                requestHttp(UrlCentre.LOGIN, params);
                break;
            case R.id.actionbar_right_button_text:
                openActivity(Ac_Register.class);
                break;
        }
    }
}
